package hadoop.mapreduce.wordcountcompress;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {

    /**
     *
     * @param key
     * @param values 分组后的一组数据
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum=0;
        //mt输出结果模板为{a,1} {a,1} {b,1} {c,1} {c,1} 分组后(values)-->{a,{1,1}} {b,1} {c,{1,1}},
        for (IntWritable count : values) {
            sum+=count.get();
        }
        //输出结果{a,2} {b,1} {c,2}
        context.write(key,new IntWritable(sum));
    }
}
